package com.seekting.autosetup;

import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.util.Log;

import org.junit.Test;

import java.io.File;

import androidx.test.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

public class BrowserTest {
    String mPackageName = "com.android.browser";

    private static final boolean DEBUG = BuildConfig.DEBUG;
    private static final String TAG = "BrowserTest";

    @Test
    public void case1() {
        File file = new File("xxxx");
        Log.d("seekting", "BrowserTest.case1()file=" + file.getAbsolutePath());
        UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());//获取设备用例

        try {
            if (!mDevice.isScreenOn()) {
                mDevice.wakeUp();//唤醒屏幕
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        //mDevice.pressHome();  //按home键

        String a = Utils.getTopActivity(mDevice);
        Log.d("seekting", "BrowserTest.case1()currentActivity" + a);


        String pkg = mDevice.getCurrentPackageName();
        Log.d("seekting", "BrowserTest.case1()" + pkg);
        if (!mPackageName.equals(pkg)) {
            startAPP(mPackageName);  //启动app
        }
        mDevice.waitForWindowUpdate(mPackageName, 5 * 2000);//等待app
        UiObject cancelButton = mDevice.findObject(new UiSelector()
                .textContains("CONTINUE"));
        try {
            if (cancelButton.exists() && cancelButton.isClickable()) {
                cancelButton.click();
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }

    }

    private void startAPP(String sPackageName) {
        Context mContext = InstrumentationRegistry.getContext();

        Intent myIntent = mContext.getPackageManager().getLaunchIntentForPackage(sPackageName);  //通过Intent启动app
        mContext.startActivity(myIntent);
    }


}
